//
//  SearchCollectionDetailsViewController.m
//  InSuger
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 Dior_3g. All rights reserved.
//

#import "SearchCollectionDetailsViewController.h"
#import "Header.h"
#import "SearchCollectionViewModel.h"
#import "BuyWebViewViewController.h"
@interface SearchCollectionDetailsViewController ()
@property(nonatomic, strong)NSMutableDictionary *searchcollectionDic;
@property(nonatomic, strong)UIScrollView *scrollView;
@property(nonatomic, strong)UIImageView *topImageView;
@property(nonatomic, strong)UIButton *button;
@property(nonatomic, strong) MBProgressHUD *progress;
@end

@implementation SearchCollectionDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"单品详情";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.view.backgroundColor = [UIColor whiteColor];
    self.progress = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _progress.labelText = @"Loading...";
    _progress.mode = MBProgressHUDModeDeterminate;
    [self createData];
}
- (void)createData{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"client_id"] = @"bt_app_android";
    dic[@"client_secret"] = @"ffcda7a1c4ff338e05c42e7972ba7b8d";
    dic[@"track_user_id"] = @"1604448";
    dic[@"oauth_token"] = @"34842fdfab6212cf103486a7fd6bdce8";
    dic[@"track_deviceid"] = @"866818029530472";
    dic[@"track_device_info"] = @"m1";
    dic[@"channel_name"] = @"ppzhushuo";
    dic[@"app_installtime"] = @"1448422581783";
    dic[@"app_versions"] = @"5.0.1";
    dic[@"os_versions"] = @"4.4.4";
    dic[@"screensize"] = @"768";
    dic[@"v"] = @"8";
    dic[@"id"] = self.str;
    dic[@"page"] = @"0";
    dic[@"pagesize"] = @"10";
    dic[@"comments_pagesize"] = @"10";
    [AFN postdataWithString:@"http://open4.bantangapp.com/product/newInfo" bodyStr:dic block:^(id block) {
        self.searchcollectionDic = block[@"data"][@"product"];

        [self createScrollView];
        self.progress.hidden = YES;
    }];
    
}
- (void)createScrollView{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * 0.925037)];
    [self.view addSubview:self.scrollView];

    self.topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.7496 * self.view.frame.size.height)];
    NSString *url = self.searchcollectionDic[@"pic"][0][@"pic"];
    [self.topImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"PlaceHolder.png"]];
    [self.scrollView addSubview:self.topImageView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0.7496 * self.view.frame.size.height, self.view.frame.size.width, 0.02999 * self.view.frame.size.height)];
    titleLabel.text = self.searchcollectionDic[@"title"];
    [self.scrollView addSubview:titleLabel];
    titleLabel.textAlignment = 1;
    
    UILabel *moneyLabel = [[UILabel alloc ] initWithFrame:CGRectMake(0, 0.77961 * self.view.frame.size.height, self.view.frame.size.width, 0.02999 * self.view.frame.size.height)];
    moneyLabel.text = [NSString stringWithFormat:@"￥%@", self.searchcollectionDic[@"price"]];
    moneyLabel.textColor = [UIColor redColor];
    moneyLabel.textAlignment = 1;
    [self.scrollView addSubview:moneyLabel];
    
    UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0.809595 * self.view.frame.size.height, self.view.frame.size.width, 0.02999 * self.view.frame.size.height)];
    descLabel.text = self.searchcollectionDic[@"desc"];
    descLabel.numberOfLines = 0;
    [descLabel sizeToFit];
    [self.scrollView addSubview:descLabel];
    
    self.scrollView.contentSize =  CGSizeMake(0, self.topImageView.frame.size.height + titleLabel.frame.size.height + moneyLabel.frame.size.height + descLabel.frame.size.height);
    
    self.button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.button.frame = CGRectMake(0, self.view.frame.size.height * 0.95502, self.view.frame.size.width, self.view.frame.size.height * 0.0449775);
    [self.button setTitle:@"立即购买" forState:(UIControlStateNormal)];
    self.button.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.button];
    [self.button addTarget:self action:@selector(buyButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    @weakify(self);
    [self addColorChangedBlock:^{
        @strongify(self);
        self.scrollView.nightBackgroundColor = UIColorFromRGB(0x343434);
        self.scrollView.normalBackgroundColor = [UIColor whiteColor];
        self.navigationController.navigationBar.nightBarTintColor = UIColorFromRGB(0x444444);
        self.navigationController.navigationBar.normalBarTintColor = [UIColor redColor];
        titleLabel.nightTextColor = [UIColor whiteColor];
        descLabel.nightTextColor = [UIColor whiteColor];
        self.view.nightBackgroundColor = UIColorFromRGB(0x343434);
        self.view.normalBackgroundColor = [UIColor whiteColor];
    }];
}
- (void)buyButtonAction:(UIButton *)button{
//    BuyWebViewViewController *buyWebViewVC = [[BuyWebViewViewController alloc] init];
//    buyWebViewVC.url = self.searchcollectionDic[@"url"];
//    [self.navigationController pushViewController:buyWebViewVC  animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
